#------------------------------------------------------------------------------
rm(list = ls())
library(LalRUtils)
libreq(
  data.table, zoo, tictoc, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggplot2, RPushbullet, ggiplot, 
  tidyverse, data.table, zoo, tictoc, fst, fixest, PanelMatch, patchwork,
  rio, magrittr, janitor, did, panelView, ggiplot, tictoc, binsreg, interflex
)

set.seed(42)
theme_set(lal_plot_theme())

notif = \(x) pbPost("note", x)

#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Define Paths
#------------------------------------------------------------------------------

# R studio
setwd( dirname(rstudioapi::getActiveDocumentContext()$path) )
# R default : unccoment if you use default R
# setwd(getSrcDirectory(function(){})[1])
#------------------------------------------------------------------------------



#------------------------------------------------------------------------------
# Load Data
#------------------------------------------------------------------------------

vcf <- fread("vcf_data_complete.csv", sep = ",")
setnames( vcf, "d", "D")
vcf_data <- copy( vcf )
gfc <- fread("gfc_dta.csv" )

#------------------------------------------------------------------------------


#------------------------------------------------------------------------------
# Figure 2: PESA Implementation Timing
#------------------------------------------------------------------------------
state_status = vcf_data[year >= 1990][ ,
                                       .(out = 1, treat = max(D)), 
                                       .(state, year) ]

f0 = panelview(out ~ treat,
               data = as.data.frame(state_status),
               index = c("state","year"),
               xlab = "Year", ylab = "State",
               main = "",
               by.timing = TRUE, legendOff = TRUE,
               background = "white")
(f0 = f0 +
    lal_plot_theme(textangle = 90) + 
    theme(legend.pos = "None", 
          axis.text = element_text( size = 35 ), 
          plot.margin = unit( c(1, 1, 1, 1), "cm") ) + 
    labs(x = "", y = "")
)
# Save figure
ggsave("main_figure2.pdf", 
       height = 10, width = 22, device = cairo_pdf)

#------------------------------------------------------------------------------
